//
//  BesSimDecryptUtil.swift
//  cangchu
//
//  Created by kang on 2022/1/25.
//

import Foundation
import UIKit

public class BesSimDecryptUtil{
    
    public static func simple64Encrypt(_ plainString:String)->String{
        
        if plainString.count <= 20{
            let plainData = plainString.data(using: String.Encoding.utf8)!
            let base64String = plainData.base64EncodedString(options: NSData.Base64EncodingOptions.init(rawValue: 0))
            let reverse = String(base64String.reversed())
            
            let hex:String = String(describing: Date().timeIntervalSinceNow).md5.uppercased()
            let hex1:String = String(describing: Date().timeIntervalSinceNow).md5.uppercased()
            
            let m:String = hex + hex1
            
            let mode = arc4random() % 10
            
            var count = 0
            var eqCount = 0
            
            var startIndex:Int = Int(mode % 2) 
            
            var changeStr:String = m
    
            for (_,_) in m.enumerated(){
                
                if count < reverse.count{
                    let char = reverse.substringInRange(Range(NSRange(location: count, length: 0))!)!
                    
                    if char != "="{
                        let rang = Range(NSRange(location: startIndex, length: 0), in: m)!
                        changeStr = changeStr.replacingCharacters(in: rang, with: char)
                    }else{
                        eqCount += 1
                    }
                    
                    count += 1
                }
                startIndex += 2
            }
            
            var substr = changeStr.substringInRange(Range(NSRange(location: 0, length: 59))!)!
            substr.append(String(describing: eqCount))
            
            if (reverse.count < 10) {
                substr.append("0")
                substr.append(String(describing: reverse.count))
            } else {
                substr.append(String(describing: reverse.count))
            }
            substr.append(String(describing: mode))
            return substr
        }
        
        return "0000"
    }
    
    public static func simple64Decrypt(_ ciphertext:String)->String{
            if (ciphertext.count != 64) {
                return ""
            } else {
                let substring = ciphertext.substringInRange(Range(NSRange(location: 60, length: 3))!)!
                let eqCount = Int((substring.substringInRange(Range(NSRange(location: 0, length: 0))!))!)!
                let count = Int(substring.substringInRange(Range(NSRange(location: 1, length: 1))!)!)!
                let mode = Int(substring.substringInRange(Range(NSRange(location: 3, length: 0))!)!)! % 2
 
                var chars = [String]()
                
                for index in 0..<count{
                    
                    if (index < eqCount) {
                        chars.append("=")
                    } else {
                        let range = Range(NSRange(location: (index * 2 + mode), length: 0))!
                        chars.append(ciphertext.substringInRange(range)!)
                    }
                }

                var resultStr = ""
                for item in chars{
                    resultStr = resultStr + item
                }
                let base64 = String(resultStr.reversed())
                return base64.base64Decoding()
            }
        }
}
